package com.chefu.system.controller;


import com.chefu.common.exception.CfException;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.pojo.R;
import com.chefu.system.pojo.Emp;
import com.chefu.system.pojo.Role;
import com.chefu.system.pojo.User;
import com.chefu.system.service.UserService;
import com.chefu.system.vo.UserVo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 分页带条件查询
     *
     * @param page
     * @param pageSize
     * @param username
     * @param name
     * @param phone
     * @return
     */
    @GetMapping("/list")
    public R findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phone", required = false) String phone
    ) {
        List<UserVo> userVoList = userService.findByPage(page, pageSize, username, name, phone);
        return R.ok().data("userVoList", userVoList);
    }

    /**
     * 查询所有状态为正常(0)的员工
     */
    @GetMapping("/findAllEmp")
    public R findAllEmp() {
        List<Emp> empList = userService.findAll();
        return R.ok().data("empList", empList);
    }

    /**
     * 查询所有角色
     */
    @GetMapping("/findAllRole")
    public R findAllRole() {
        List<Role> roleList = userService.findAllRole();
        return R.ok().data("roleList", roleList);
    }

    /**
     * 添加用户
     */
    @PostMapping("/create")
    public R createUser(
            @RequestParam("roleId") Long roleId,
            @Valid User user, BindingResult result
    ) {
        if (result.hasErrors()) {
            //取出错误信息
            String errorMsg = result.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        userService.create(roleId, user);
        return R.ok();
    }

    /**
     * 根据用户id查询用户
     */
    @GetMapping("/findById")
    public R findById(@RequestParam("id") Long id) {
        User user = userService.findById(id);
        return R.ok().data("user", user);
    }

    /**
     * 修改用户
     */
    @PutMapping("/update")
    public R update(@RequestParam("roleId") Long roleId,
                    @Valid User user, BindingResult result
    ) {
        if (result.hasErrors()) {
            //取出错误信息
            String errorMsg = result.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        userService.updateByUser(roleId, user);
        return R.ok();
    }

    /**
     * 锁定用户
     */
    @PutMapping("/lock")
    public R lockUser(@RequestParam("userId") Long userId, @RequestParam("status") Integer status) {
        userService.lock(userId, status);
        return R.ok();
    }


    @GetMapping("/query")
    public ResponseEntity<User> findUserByNameAndPwd(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = userService.findUserByNameAndPwd(username, password);
        return ResponseEntity.ok(user);
    }
}

